package com.qeegoo.b2bautozityp.moudle.goods.viewmodel;

//┏┓　　　┏┓ 
//┏┛┻━━━┛┻┓ 
//┃　　　　　　　┃ 　 
//┃　　　━　　　┃ 
//┃　┳┛　┗┳　┃ 
//┃　　　　　　　┃ 
//┃　　　┻　　　┃ 
//┃　　　　　　　┃ 
//┗━┓　　　┏━┛ 
//┃　　　┃  神兽保佑　　　　　　　　 
//┃　　　┃  代码无BUG！ 
//┃　　　┗━━━┓ 
//┃　　　　　　　┣┓ 
//┃　　　　　　　┏┛ 
//┗┓┓┏━┳┓┏┛ 
// ┃┫┫　┃┫┫ 
// ┗┻┛　┗┻┛

import android.databinding.ObservableField;
import android.os.Bundle;
import android.view.View;

import com.kelin.mvvmlight.command.ReplyCommand;
import com.kelin.mvvmlight.messenger.Messenger;
import com.qeegoo.b2bautozityp.R;
import com.qeegoo.b2bautozityp.api.HttpParams;
import com.qeegoo.b2bautozityp.api.ProgressSubscriber;
import com.qeegoo.b2bautozityp.api.RequestApi;
import com.qeegoo.b2bautozityp.api.RetrofitService;
import com.qeegoo.b2bautozityp.moudle.base.BaseAdapter;
import com.qeegoo.b2bautozityp.moudle.good.view.GoodsActivity;
import com.qeegoo.b2bautozityp.moudle.goods.model.GoodsManagerBean;
import com.qeegoo.b2bautozityp.moudle.goods.view.GoodsManagerFragment;

import base.lib.util.ActivityManager;
import base.lib.util.NavigateUtils;
import base.lib.util.ToastUtils;

/**
 * Created by lk on 2017/9/25.
 */

public class GoodItemViewModel {

    private final BaseAdapter adapter;
    private RequestApi requestApi;
    public GoodsManagerBean.GoodDetailBean detailBean = new GoodsManagerBean.GoodDetailBean();
    public ObservableField<Integer> isVisible = new ObservableField<>(View.GONE);
    public ObservableField<Integer> position = new ObservableField<>(-1);
    public ObservableField<Boolean> isSelected = new ObservableField<>(false);
    public ObservableField<String> imageUrl = new ObservableField<>();
    public ObservableField<Integer> background = new ObservableField<>(R.color.blue_button_center);
    public ObservableField<String> goodStatus = new ObservableField<>();
    public ObservableField<String> goodsInfo = new ObservableField<>();
    public ObservableField<String> goodCount = new ObservableField<>();
    public ObservableField<String> goodFlag = new ObservableField<>();
    public ObservableField<Integer> conferenceVisible = new ObservableField<>(View.GONE);
    public ObservableField<Integer> putUpVisible = new ObservableField<>(View.GONE);
    public ObservableField<Integer> takeOffVisible = new ObservableField<>(View.GONE);
    public ObservableField<Integer> delVisible = new ObservableField<>(View.GONE);
    public ObservableField<Integer> editVisible = new ObservableField<>(View.GONE);
    public ObservableField<Integer> goodStatusVisible = new ObservableField<>(View.GONE);


    public ReplyCommand selectItemCommand = new ReplyCommand(() -> {
        detailBean.isSelect = !isSelected.get();
        isSelected.set(!isSelected.get());
        Messenger.getDefault().send(detailBean, GoodsManagerFragment.class.getSimpleName());

    });
    public ReplyCommand operatorCommand = new ReplyCommand(() -> {
        int status = detailBean.status == 9 ? 2 : 1;
        requestApi.updateGoodsStatus(HttpParams.paramUpdateGoodsStatus(detailBean.id, status + ""))
                .map(new RetrofitService.HttpResultFunc())
                .compose(RetrofitService.applySchedulers())
                .subscribe(new ProgressSubscriber<Object>() {
                    @Override
                    public void onNext(Object r) {
                        String msg = status == 1 ? "上架成功！" : "下架成功";
                        ToastUtils.showToast(msg);
                        adapter.remove(position.get());
                        adapter.notifyItemRemoved(position.get());
                        if (position.get() != adapter.getData().size())
                            adapter.notifyItemRangeChanged(position.get(), adapter.getData().size() - position.get());
                    }
                });
    });
    public ReplyCommand delCommand = new ReplyCommand(() -> {
        requestApi.updateGoodsStatus(HttpParams.paramUpdateGoodsStatus(detailBean.id, "0"))
                .map(new RetrofitService.HttpResultFunc())
                .compose(RetrofitService.applySchedulers())
                .subscribe(new ProgressSubscriber<Object>() {
                    @Override
                    public void onNext(Object r) {
                        ToastUtils.showToast("删除成功！");
                        adapter.remove(position.get());
                        adapter.notifyItemRemoved(position.get());
                        if (position.get() != adapter.getData().size())
                            adapter.notifyItemRangeChanged(position.get(), adapter.getData().size() - position.get());
                    }
                });
    });
    public ReplyCommand editCommand = new ReplyCommand(() -> {
        Bundle bundle = new Bundle();
        bundle.putString("goodsId", detailBean.id);
        NavigateUtils.startActivity(GoodsActivity.class, bundle);
    });

    public GoodItemViewModel(GoodsManagerBean.GoodDetailBean detailBean, RequestApi requestApi, BaseAdapter adapter) {
        this.requestApi = requestApi;
        this.detailBean = detailBean;
        this.adapter = adapter;
        imageUrl.set(detailBean.goodsImagePath);
        goodStatus.set(detailBean.statusCaption);
        goodsInfo.set(detailBean.name);
        goodCount.set("库存：" + detailBean.quantity);
        goodFlag.set(detailBean.productTypeText);
        goodStatusVisible.set(detailBean.isShowStatus ? View.VISIBLE : View.GONE);
        isSelected.set(detailBean.isSelect);
        isVisible.set(detailBean.isShowBtn ? View.VISIBLE : View.GONE);
        goodStatusVisible.set(detailBean.pageType == 1 ? View.VISIBLE : View.GONE);
        goodStatus.set(detailBean.statusCaption);
        switch (detailBean.status) {
            case 9://上架
                putUpVisible.set(View.GONE);
                takeOffVisible.set(View.VISIBLE);
                conferenceVisible.set(View.GONE);
                delVisible.set(View.GONE);
                editVisible.set(View.GONE);
                background.set(ActivityManager.getActivity().getResources().getColor(R.color.color_yellow));
                break;

            case 21://下架
                putUpVisible.set(View.VISIBLE);
                takeOffVisible.set(View.GONE);
                conferenceVisible.set(View.GONE);
                delVisible.set(View.VISIBLE);
                editVisible.set(View.VISIBLE);
                background.set(ActivityManager.getActivity().getResources().getColor(R.color.blue2));
                break;

            case 22://售罄
                putUpVisible.set(View.GONE);
                takeOffVisible.set(View.GONE);
                conferenceVisible.set(View.VISIBLE);
                delVisible.set(View.VISIBLE);
                editVisible.set(View.GONE);
                background.set(ActivityManager.getActivity().getResources().getColor(R.color.color_9c9c9c));
                break;

            default:
                break;
        }
    }


}
